<template>
  <div>
    <el-card>
      <el-page-header @back="$router.back()" content="🐱猫咪列表" />
    </el-card>
    <el-card class="mt-20">
      <div slot="header">
        <span>猫咪列表</span>
        <el-button size="mini" type="primary" style="float:right;" @click="show=true">新增猫咪</el-button>
      </div>

      <el-table :data="list" stripe border v-loading="tableLoading">
        <el-table-column label="名字" prop="name" />
        <el-table-column label="年龄" prop="age" />
        <el-table-column label="介绍" prop="breed" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-popconfirm :title="`确定删除${scope.row.name}吗？`" @onConfirm="remove(scope)">
              <el-button size="mini" type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :visible.sync="show" center title="猫咪设置">
        <el-form :model="form" :rules="rules" ref="ruleForm" size="mini" label-width="90px">
          <el-form-item label="猫的名字" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="猫的年龄" prop="age">
            <el-input type="number" v-model.number="form.age" />
          </el-form-item>
          <el-form-item label="饲养介绍" prop="breed">
            <el-input type="textarea" v-model="form.breed" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" type="primary" @click="save()">保存</el-button>
          <el-button size="small" @click="show=false;form={};">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>

export default {
  data() {
    return {
      activeName: undefined,
      show: false,
      tableLoading: false,
      list: [],
      form: {
        name: "",
        age: undefined,
        breed: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名字", trigger: "blur" }],
        age: [
          {
            required: true,
            type: "number",
            message: "请输入年龄",
            trigger: "blur"
          }
        ]
      }
    };
  },

  methods: {
    async getData() {
      this.tableLoading = true
      const { data } = await this.axios.get("http://localhost:3000/cats", {},  { timeout: 3000 });
      this.list = data;
      this.tableLoading = false
    },

    save(data = this.form) {
      this.$refs["ruleForm"].validate(vali => {
        if (vali) {
          this.axios.post("http://localhost:3000/cats", data).then(res => {
            if (res) {
              this.getData();
              this.show = false;
              this.form = {};
            }
          });
        }
      });
    },

    remove(params) {
      this.axios.delete(`http://localhost:3000/cats/${params.$index}`).then(() => {
        this.getData();
      });
    }
  },
  created() {
    this.getData();
  }
};
</script>

<style>
</style>